﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;

namespace BusinessLogic.Command
{
    public class MaintainLanguageCommand : BaseCommand<object>
    {
        private List<APP_LANGUAGE> _list;

        public MaintainLanguageCommand(List<APP_LANGUAGE> langList)
        {
            _list = langList;
        }

        public override void Command()
        {
            CubeOfDB2Entities ctx = new CubeOfDB2Entities();

            foreach (APP_LANGUAGE row in _list)
            {
                if (row.act == ActionEnum.Add || (row.ID == 0 && row.act != ActionEnum.Delete) || (row.ID == null && row.act != ActionEnum.Delete))
                {
                    ctx.APP_LANGUAGE.AddObject(row);
                }
                else if (row.act == ActionEnum.Edit)
                {
                    APP_LANGUAGE e = ctx.APP_LANGUAGE.Where(o => o.ID == row.ID).FirstOrDefault();
                    if (e != null)
                    {
                        e.PERSONAL_ID = row.PERSONAL_ID;
                        e.LANG_NAME = row.LANG_NAME;
                        e.READING = row.READING;
                        e.SPEAKING = row.SPEAKING;
                        e.UNDERSTAND = row.UNDERSTAND;
                        e.WRITING = row.WRITING;
                    }
                }
                else if (row.act == ActionEnum.Delete)
                {
                    APP_LANGUAGE e = ctx.APP_LANGUAGE.Where(o => o.ID == row.ID).FirstOrDefault();
                    if (e != null)
                    {
                        ctx.APP_LANGUAGE.DeleteObject(e);
                    }
                }

                ctx.SaveChanges();
            }
        }
    }
}
